﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Ionic.Zip;
using iLangl.Transport;


namespace iLangl.Transport.Communication
{
	public class BaseCommunicationManager : BaseManager, ICommunicationManager
	{
		protected ITransport _transport;

		public BaseCommunicationManager(ITransport transport)
			: base()
		{
			_transport = transport;
		}
		
		#region ICommunicationManager Members

		public virtual void SentPackage(IPackage package)
		{
			if (package.IsSynced())
			{
				UploadLightRequest ulr = new UploadLightRequest();

				ulr.LicenseKey = package.PluginLiceneKey;
				ulr.PackageId = package.Id;
				ulr.DeliveredFileName = package.DeliveryPackageFileName;
				ulr.PackageData = Facade.Instance.PackageManager.PatchPackageToString(package);

				UploadLightResponse ulresponse = _transport.SendRequestSync(ulr);

				foreach (String response in ulresponse.Responses)
				{
					IPackage responsepackage = Facade.Instance.PackageManager.DispatchPackageFromString(response);
					Facade.Instance.PackageManager.ExecutePackage(responsepackage);
				}
			}
			else
			{
				Facade.Instance.PackageManager.PatchPackageToFile(TransportConfiguration.Instance.OutcomePackageFolder, package);
			}
		}

		#endregion


		public IPackage GetPackage()
		{
			throw new NotImplementedException();
		}

		public ITransport Transport
		{
			get
			{
				return _transport;
			}
			set
			{
				_transport = value;
			}
		}
	}
}
